﻿using SendGrid;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace CloudStorageLight.Core
{
    public interface IMailService
    {
        Task SendMailAsync(MailMessage message);
        Task SendMailAsync(MailAddress from, IEnumerable<MailAddress> to, IEnumerable<MailAddress> cc, IEnumerable<MailAddress> bcc, string subject, string body, bool isHtml);   
    }

    public static class MailServiceFactory
    {
        public static IMailService GetService()
        {
            if (SystemSettings.Instance.UseMySmtpServer)
            {
                return new SmtpMailService();
            }
            else
            {
                return new SendGridMailService();
            }
        }
    }

    public abstract class MailService : IMailService
    {

        public Task SendMailAsync(MailMessage message)
        {
            return SendMailAsync(message.From, message.To, message.CC, message.Bcc, message.Subject, message.Body, message.IsBodyHtml);
        }

        public abstract Task SendMailAsync(MailAddress from, IEnumerable<MailAddress> to, IEnumerable<MailAddress> cc, IEnumerable<MailAddress> bcc, string subject, string body, bool isHtml);
    }

    public class SmtpMailService : MailService
    {

        public override async Task SendMailAsync(MailAddress from, IEnumerable<MailAddress> to, IEnumerable<MailAddress> cc, IEnumerable<MailAddress> bcc, string subject, string body, bool isHtml)
        {
            using(var smtp = new SmtpClient())
            {
                var myMessage = new MailMessage();
                myMessage.From = from;
                if (to != null) foreach (var a in to) myMessage.To.Add(a);
                if (cc != null) foreach (var a in cc) myMessage.CC.Add(a);
                if (bcc != null) foreach (var a in bcc) myMessage.Bcc.Add(a);
                myMessage.Subject = subject;
                myMessage.Body = body;
                myMessage.IsBodyHtml = isHtml;
                smtp.Send(myMessage);
            }

            await Task.Delay(0);
        }
    }

    public class SendGridMailService : MailService
    {
        public override async Task SendMailAsync(MailAddress from, IEnumerable<MailAddress> to, IEnumerable<MailAddress> cc, IEnumerable<MailAddress> bcc, string subject, string body, bool isHtml)
        {
            var myMessage = new SendGridMessage();
            myMessage.From = from;
            if (to != null) foreach (var a in to) myMessage.AddTo(a.Address);
            if (cc != null) foreach (var a in cc) myMessage.AddCc(a.Address);
            if (bcc != null) foreach (var a in bcc) myMessage.AddBcc(a.Address);
            myMessage.Subject = subject;
            if (isHtml)
            {
                myMessage.Html = body;
            }
            else
            {
                myMessage.Text = body;
            }
            var credentials = new NetworkCredential(
                       SystemSettings.Instance.MailUser, SystemSettings.Instance.MailPassword);

            // Create a Web transport for sending email.
            var transportWeb = new SendGrid.Web(credentials);
            await transportWeb.DeliverAsync(myMessage);
        }
    }
}
